const router = require("express").Router();
const mysqlhandle = require("../routehandles/mysqlhandle");
const fs = require("fs-extra")
const path = require("path")

//新建任务图片文件夹
function createFolder(fname){
    var fldr = "";
	var fso = new ActiveXObject("Scripting.FileSystemObject");
    fldr = "../public/picture/"+fname
	if(!fso.FolderExists(fldr)){     //判断文件夹是否存在，不存在就创建
		fso.CreateFolder (fldr);
	}
    return fldr
}

function mkdir(dirname) {

    if (fs.existsSync(dirname)) {
        console.log("存在"+dirname)

      return true;

    } else {

      if (mkdir(path.dirname(dirname))) {

        console.log("新建"+dirname)
        fs.mkdirSync(dirname);

        return true;

      }

    }

  }

//创建任务函数
router.post('/createtask',async (req,res) => {
    const params = req.body;
    var xx = await mysqlhandle.sUAPTable(params.username);
    
    var yy = await mysqlhandle.sPublishTableAll()
    var tasknum = yy.length+1;
    var picurl = path.join(path.dirname(__dirname)+"/public/picture",String(params.username) +"p"+ String(tasknum))
    mkdir(picurl)
    //var picurl = createFolder(String(params.username)+String(tasknum))
    console.log(picurl)
    var begintime = new Date().toLocaleDateString().split('/').map(item=>{if (item<10){return '0'+ item}else {return item}}).join('-')
    console.log("begintime:"+begintime)
    if(params.tag == 'none')
    {
        console.log(params.tag)
        console.log('上面的是tag')
        return res.json({msg:'请选择标签',result:false})
    }else if (params.endtime == '' || params.endtime == null)
    {
        return res.json({msg:'请选择结束日期',result:false})
    }else if(params.contact == '' ||params.contact == null)
    {
        return res.json({msg:'请填写联系方式',result:false})
    }else if (params.taskinfo =='' || params.taskinfo == null)
    {
        return res.json({msg:'请填写详细需求描述',result:false})
    }else{
        nickname = xx[0]["nickname"];
        var userinfo = await mysqlhandle.sUAPTable(params.username)
        var balance = parseFloat(userinfo[0]["balance"]) //获取发布者余额
        if(balance-parseInt(params.picnum)*0.2>=0){
            mysqlhandle.iPublishTable(params.username,nickname,tasknum,params.tag,params.taskinfo,picurl,begintime,params.endtime,"进行中",params.contact,params.picnum);
            mysqlhandle.uUAPTable("balance",balance-parseInt(params.picnum)*0.2,params.username)//修改发布任务者的余额
            return res.json({msg:'创建任务成功！',result:true})
        }
       else{
        return res.json({msg:'余额不足',result:false})
       }
    }
})

router.post('/getinfor',async (req,res)=>{
    const params = req.body;
    var task =await mysqlhandle.sPublishTableById(params.tasknum)
    var getpic =await mysqlhandle.sPushTableBytasknum(params.tasknum)
    var totalnum = parseInt(task[0]['picnum'])
    var remainnum = totalnum - getpic.length
    var remainmoney = parseFloat((remainnum*2)/10)
    res.json({num:getpic.length,money:remainmoney})
})

//结束任务函数
router.post('/finishtask',async (req,res) => {
    const params = req.body;
    mysqlhandle.uPublishTable('status','已结束',params.tasknum);
    console.log('jjll')
    var result =await mysqlhandle.sUAPTable(params.username)
    var resultbalance = result[0]['balance']
    var task =await mysqlhandle.sPublishTableById2(params.tasknum)
    var getpic =await mysqlhandle.sPushTableBytasknum(params.tasknum)
    var totalnum = parseInt(task[0]['picnum'])
    var remainnum = totalnum - getpic.length
    var remainmoney = parseFloat((remainnum*2)/10)
    console.log('hhhhllll')
    console.log(remainmoney)
    console.log(resultbalance)
    mysqlhandle.uUAPTable('balance',parseFloat(resultbalance)+remainmoney,params.username)
})

module.exports = router

